package org.oexam.ostudy.facade;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import javax.naming.NamingException;

import org.oexam.ostudy.db.TestAccess;
import org.oexam.ostudy.db.UsersAccess;
import org.oexam.ostudy.facade.struct.InsertNewQuestionDetails;
import org.oexam.ostudy.facade.struct.TestDisplayDetails;
import org.oexam.ostudy.facade.struct.TestNameAndId;
import org.oexam.ostudy.facade.struct.ViewAndAddQuestionsPageDisplayDetails;
import org.oexam.ostudy.sl.ManageTests;
import org.oexam.ostudy.struct.AllResults;
import org.oexam.ostudy.struct.StudentAnswers;

public class ManageTestsPages {
	
	public String addQuestionToAQuestionSet(InsertNewQuestionDetails questionDetails){
		
		if(questionDetails.getAnswer()==0||
				questionDetails.getQuestion().equals("")||questionDetails.getQuestion()==null||
				questionDetails.getOption1().equals("")||questionDetails.getOption1()==null||
				questionDetails.getOption2().equals("")||questionDetails.getOption2()==null||
				questionDetails.getOption3().equals("")||questionDetails.getOption3()==null||
				questionDetails.getOption4().equals("")||questionDetails.getOption4()==null){
			
			return "All fields are mandatory.Please enter all fields";
		}
		
		if(15>questionDetails.getQuestion().length()){
			return "Minimum length of the question is 15";
		}
		
		try {
			return new ManageTests().addQuestionToTest(questionDetails);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}
		
	}
	
	public List<TestNameAndId> getAllTestsLinkedToTeacher(int userID) throws SQLException, NamingException{
		
		return new ManageTests().getAllTestsLinkedToTeacher(userID);
	}
	
	public ViewAndAddQuestionsPageDisplayDetails getVAQuestionPageDetails(int testID) throws SQLException, NamingException{
		
		return new ManageTests().getVAQuestionPageDetails(testID);
	}
	
   public List<TestNameAndId> getAllTestsThatStudentIsEligibleFor(int userID) throws SQLException, NamingException{
	   
	   // to resolve concurrent access issue. calls to methods have been changed here. must resolve and minimize errors while accessing db
	   return new TestAccess().getAllTests();
   }
   
   public TestDisplayDetails startTestAndGetTestData(int testID, int userID ) throws Exception{
	   
	   int studentID = new UsersAccess().getStudentIDUsingUserID(userID);
	   ManageTests manageTests = new ManageTests();
	   
	   TestDisplayDetails details = manageTests.getTestData(testID, studentID);
	   if(details.getQuestionList().size()<=0){
		   throw new Exception("No questions present int the test");
	   }
	   manageTests.startTest(testID,studentID);
	   
	   // Not tested properly
	   Collections.shuffle(details.getQuestionList());
	  
	   return details;
   }
   
   public String finalizeStudentAnswers(TestDisplayDetails testData) throws SQLException, NamingException{
	   
	   // TODO Prasanna: Avoid direct DB call from here.
	   List<StudentAnswers> answers = testData.getAnswers();
	   TestAccess testAccess = new TestAccess();
	   for(StudentAnswers answer: answers){
		   try {
			answer.setAid(testAccess.getLastAnswerID()+1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   testAccess.insertStudentAnswer(answer);
		   
	   }
	   
	 int result = new ManageTestsPages().finishAndGetResultForTest(answers);
	 
	 AllResults finalresult = new AllResults();
	 TestAccess tAccess = new TestAccess();
	 finalresult.setResult_id(tAccess.getLastResultID()+1);
	 
	 finalresult.setResult(result);
	 finalresult.setStudent_id(testData.getStudentID());
	 finalresult.setTest_id(testData.getTestNameAndId().getId());
	 tAccess.insertResult(finalresult);
	   
	   return "Answers Finalized. Result displayed is only tentative  <br>  : " + result;
   }
   

   public int finishAndGetResultForTest(List<StudentAnswers> answers) throws SQLException, NamingException{
	   
	   return new ManageTests().addAndResult(answers);
   }

}
